package com.hulk.other;

import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Test {
	public static void main(String[] args) {
		int count = 10;
		CyclicBarrier cyclicBarrier = new CyclicBarrier(count);
		ExecutorService executorService = Executors.newFixedThreadPool(count);
		for (int i = 0; i < count; i++)
			executorService.execute(new Test().new Task(cyclicBarrier));
		executorService.shutdown();
		int i = 0;
		while (!executorService.isTerminated()) {
			
			try {
				Thread.sleep(10);
				System.out.println(i);
				i++;
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	public class Task implements Runnable {
		private CyclicBarrier cyclicBarrier;

		public Task(CyclicBarrier cyclicBarrier) {
			this.cyclicBarrier = cyclicBarrier;
		}

		@Override
		public void run() {
			try { // 等待所有任务准备就绪
				cyclicBarrier.await(); // 测试内容
				System.out.println("开始测试");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
